package com.yuyou.tas.lesson.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.yuyou.tas.lesson.mapper.BaseSectionSubjectRelMapper;
import com.yuyou.tas.lesson.mapper.LessonMapper;
import com.yuyou.tas.lesson.service.IBaseChapterService;
import com.yuyou.tas.lesson.service.IBaseLevelService;
import com.yuyou.tas.lesson.service.IBaseSectionService;
import com.yuyou.tas.lesson.service.IBaseTermService;
import com.yuyou.tas.lesson.service.ICommDataConfigService;
import com.yuyou.tas.lesson.service.ILessonChapterService;
import com.yuyou.tas.lesson.service.ILessonClassChapterService;
import com.yuyou.tas.lesson.service.ILessonClassService;
import com.yuyou.tas.lesson.service.ILessonMaterialService;
import com.yuyou.tas.lesson.service.ILessonService;
import com.yuyou.tas.lesson.service.comm.OfficeService;
import com.yuyou.tas.lesson.service.comm.OrganLimitService;
import com.yuyou.tas.lesson.service.comm.Subj7SectLimitService;
import com.yuyou.tas.service.api.admin.entity.CommDataConfig;
import com.yuyou.tas.service.api.lesson.dto.LessonDetailDTO;
import com.yuyou.tas.service.api.lesson.entity.BaseSection;
import com.yuyou.tas.service.api.lesson.entity.BaseSectionSubjectRel;
import com.yuyou.tas.service.api.lesson.entity.BaseTrimester;
import com.yuyou.tas.service.api.lesson.entity.Lesson;
import com.yuyou.tas.util.bean.ResBoolSimpleInfo;
import com.yuyou.tas.util.enums.lesson.LessonBaseEnums;
import com.yuyou.tas.util.enums.lesson.LessonEnums;
import com.yuyou.tas.util.exception.ResBoolSimpleInfoException;

@Service
public class LessonServiceAopImpl{

	@Autowired
	private LessonMapper lessonMapper;
	
    @Autowired
    private BaseSectionSubjectRelMapper baseSectionSubjectRelMapper;
	
	@Autowired
	private Subj7SectLimitService subj7SectLimitService;
	
	@Autowired
	private OrganLimitService organLimitService;
		
	@Autowired
	private IBaseSectionService baseSectionService;
	
	@Autowired
	private IBaseLevelService baseLevelService;
	
	@Autowired
	private IBaseTermService baseTermService;
	@Autowired
	private ICommDataConfigService commDataConfigService;
	
	@Autowired
	private ILessonChapterService lessonChapterService;
	
	@Autowired
	private ILessonMaterialService lessonMaterialService;
	
	@Autowired
	private IBaseChapterService baseChapterService;
	
	@Autowired
	private ILessonClassChapterService lessonClassChapterService;
	
	@Autowired
	private OfficeService officeService;
	
	
	@Autowired
	private ILessonClassService lessonClassService;
	@Autowired
	private ILessonService lessonService;
	
	@Transactional(propagation=Propagation.REQUIRES_NEW,rollbackFor=Exception.class,timeout=60)
	public ResBoolSimpleInfo<LessonDetailDTO> syncLesson(Lesson record) throws Exception {	
		Lesson sycnLesson=lessonMapper.selectByOrgan7OId(record.getoId(), record.getAuthOrganizationId());
 		
		//年级转换
		BaseSection baseSection=baseSectionService.selectBaseSectionById(record.getBaseSectionId());
		if(baseSection==null){
			throw new ResBoolSimpleInfoException("课程("+record.getoId()+")section("+record.getBaseSectionId()+")的年级不存在");
		}
		record.setBaseSectionId(baseSection.getId());
		//科目转换
		CommDataConfig subject=commDataConfigService.selectByCode(String.valueOf(record.getDataSubject()));
		if(subject==null){
			throw new ResBoolSimpleInfoException("课程("+record.getoId()+")subject("+record.getDataSubject()+")的科目不存在");
		}
		record.setDataSubject(subject.getId());
		
		//学期转换
		if(record.getBaseTrimesterId()!=null){
			BaseTrimester baseTrimester=baseTermService.selectByOrgan7OId(record.getBaseTrimesterId(), record.getAuthOrganizationId());
			if(baseTrimester ==null){
				throw new ResBoolSimpleInfoException("课程("+record.getoId()+")trimester("+record.getBaseTrimesterId()+")的学期不存在");
			}
			record.setBaseTrimesterId(baseTrimester.getId());			
		}

		
		
		//检查科目对应年级关系是否正确
		Map<String , Object> condition=new HashMap<>();
		condition.put("isUse", LessonBaseEnums.isEnableed.enabl.getValue());
		condition.put("isStan", 1);//获取标准数据
		condition.put("subjectIds", new String[]{String.valueOf(subject.getId())});
		List<BaseSectionSubjectRel> bssList = baseSectionSubjectRelMapper.findSSRs(condition);		
		if(bssList.size()==0){
			throw new ResBoolSimpleInfoException("课程("+record.getoId()+")subject("+record.getDataSubject()+")的对应年级"+record.getBaseSectionId()+"不正确");
		}
		List<String> sectionList = Stream.of(bssList.get(0).getBaseSectionId().split(",")).collect(Collectors.toList());
		if(!sectionList.contains(String.valueOf(baseSection.getId()))){
			throw new ResBoolSimpleInfoException("课程("+record.getoId()+")subject("+record.getDataSubject()+")的对应年级"+record.getBaseSectionId()+"不正确");
		}
		
		
		
		//层次，保存code
		record.setBaseLevelId(record.getBaseLevelId());
		
		//教材版本
		if(record.getBaseTeachId()!=null){
			CommDataConfig teach=commDataConfigService.selectByCode(String.valueOf(record.getBaseTeachId()));
			if(teach==null){
				throw new ResBoolSimpleInfoException("课程("+record.getoId()+")teach("+record.getBaseTeachId()+")的教材版本不存在");
			}
			record.setBaseTeachId(teach.getId());
		}						
		if(sycnLesson !=null){
			//课程发布不能修改
			if(sycnLesson.getStatus().equals(LessonEnums.status.pass.getValue())){
				return new ResBoolSimpleInfo<LessonDetailDTO>(true,null,null);
			}
			//讲次不能修改
			record.setpId(null);
			record.setChapterNum(null);
//			record.setDataSubject(null);	
//			record.setBaseSectionId(null);
			record.setId(sycnLesson.getId());
			lessonService.updateByPrimaryKeySelective(record);
		}else{	
			record.setStatus(LessonEnums.status.init.getValue());
			lessonService.insert(record);	
		}	
		return new ResBoolSimpleInfo<LessonDetailDTO>(true,null,null);
	}
	
	
	
}